package com.mdp.workflow.bus.candidate.strategy;

import com.mdp.core.err.BizException;
import com.mdp.core.utils.ObjectTools;
import com.mdp.core.utils.StrUtils;
import com.mdp.safe.client.entity.Dept;
import com.mdp.safe.client.entity.User;
import com.mdp.safe.client.utils.LoginUtils;
import com.mdp.workflow.bus.candidate.TaskCandidateStrategy;
import com.mdp.workflow.client.service.WfSysUserService;
import com.mdp.workflow.comm.dict.CandidateStrategy;
import org.flowable.engine.delegate.DelegateExecution;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * 9哥
 *
 *@author 唛盟开源9哥
 */
@Component
public class PreDeptManagerStrategy implements TaskCandidateStrategy {

    @Autowired
    WfSysUserService wfSysUserService;

    @Override
    public CandidateStrategy getStrategy() {
        return CandidateStrategy.PRE_DEPT_MANAGER;
    }

    @Override
    public void validateParam(String param) {

    }

    /**
     * 如果是驳回后从新分配任务，就没法找到上一步是谁，需要遍历当前节点的上一级节点，找到上一步的执行人，这开源版不支持，放到商业版吧
     *
     * @param execution 执行任务
     * @param param
     * @return
     */
    @Override
    public Set<String> calculateUsers(DelegateExecution execution, String param) {
        User user = LoginUtils.getCurrentUserInfo();
        Dept dept=wfSysUserService.getDept(user.getDeptid());
        if(dept==null){
            throw new BizException("dept-not-exists","部门%s不存在",user.getDeptid());
        }
        if(ObjectTools.isEmpty(dept.getManager())){
            throw new BizException("dept-manager-not-set","部门%s未设置部门负责人",dept.getDeptName());
        }
        return StrUtils.splitToStringSet(dept.getManager());
    }
 

    @Override
    public boolean isParamRequired() {
        return false;
    }



}
